package StudyCode.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

/**
 * @author lixiaoyao
 */
public class DrawTest {
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				JFrame frame=new DrawFrame();
				frame.setTitle("DrawTest");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

/*

 */
class DrawFrame extends JFrame {
	public DrawFrame() {
		add(new DrawComponent());
		pack();
	}
}

class DrawComponent extends JComponent {
	private static final int DEFAULT_WIDTH = 400;
	private static final int DEFAULT_HEIGHT = 400;

	protected void paintComponent(Graphics graphics) {
		Graphics2D graphics2D = (Graphics2D) graphics;

		// draw a rectangle

		double leftX = 100;
		double topY = 100;
		double width = 200;
		double height = 150;

		Rectangle2D rectangle2D = new Rectangle2D.Double(leftX, topY, width, height);
		graphics2D.draw(rectangle2D);

		// draw the enclosed ellipse
		Ellipse2D ellipse2D = new Ellipse2D.Double();
		ellipse2D.setFrame(rectangle2D);
		graphics2D.draw(ellipse2D);

		// draw a diagonal line
		graphics2D.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height));

		// draw a circle with the same center
		double centerX = rectangle2D.getCenterX();
		double centerY = rectangle2D.getCenterY();
		double radius = 150;

		Ellipse2D circle = new Ellipse2D.Double();
		circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
		graphics2D.draw(circle);
	}

	public Dimension getPreferredSize() {
		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

}